﻿using UnityEngine;
using System.Collections;

public class Monster : MonoBehaviour {
	public float HP = 100f;
	
	public float speedX = 2.5f;
	public float speedY = 2f;
	public float jumpDuration = 0.2f;

	//value for reference after animating/action ends
	public float default_moveDuration = 0.5f;
	public float default_attackDelay = 0.3f;
	public float defaultSpeedX = 2.5f;

	public float countDown = 0f;
	public float moveDuration = 0f;
	public float dyingAnimationCountDown = 0f;

	//Local Variable
	//public ArrayList actionTracker;
	public int curAction;
	public bool isJump=false;
	public bool isFalling=false;
	public bool isGrounded=false;
	public bool isMoving=false;
	public bool isDying=false;

	//Variable for reference, DO NOT MODIFY THE VALUE
	private Animator animator;


	void Start () {
		animator = this.GetComponent<Animator>();
		//actionTracker = new ArrayList();

	}

	void FixedUpdate () {
		/*
		 * 1 = run
		 * 2 = jump
		 * 3 = attack
		 * 4 = charge
		 */

		curAction = 0;

		//1. if should is moving
		if(isMoving && moveDuration <=0){
			moveDuration = default_moveDuration;
			isJump = true;
			isGrounded = false;
			countDown = jumpDuration;
		}

		if(moveDuration > 0){
			moveDuration -= Time.deltaTime;
		}
		
		//countDown is a variable mainly used for manual-animation/move
		if(countDown > 0){
			countDown -= Time.deltaTime;
		}else{
			
			if(isJump){
				isJump = false;
				isFalling = true;
			}
		}
		if(isJump || isFalling){
			curAction = 2;
		}else if(!isGrounded){
			isFalling = true;
		}

		if(isJump){
			transform.position = new Vector2(transform.position.x+(speedX*Time.deltaTime), transform.position.y+(speedY*Time.deltaTime));
		}else if(isFalling){
			float newX = transform.position.x;
			if(isMoving){
				newX += (speedX*Time.deltaTime);
			}
			transform.position = new Vector2(newX, transform.position.y-(speedY*Time.deltaTime));
		}else{
		}

		//2. set sprite animation
		animator.SetInteger("characterAction", curAction);

		//3. if HP reached 0
		if( isDying ){
			if(dyingAnimationCountDown > 0){
				float alpha = GetComponent<SpriteRenderer>().color.a;
				GetComponent<SpriteRenderer>().color = new Color(0f, 0f, 0f, (alpha-(0.5f*Time.deltaTime)) ); // Set to opaque black
				dyingAnimationCountDown -= Time.deltaTime;
			}else{
				removeMonsterFromScreen();
			}
		}
	}

	//collision events have: Enter2D, Stay2D, Exit2D
	//May need to use OnCollisionStay2D() for constant damage
	void OnCollisionEnter2D(Collision2D collisionObj){
		string collidedObjTagName = collisionObj.gameObject.tag;
		Debug.Log("Monster OnCollisionEnter2D(), collidedObj tag: " + collidedObjTagName);
		if( "Structures".Equals(collisionObj.gameObject.tag) && "Ground".Equals(collisionObj.gameObject.name) ){
			isGrounded = true;
			if(isFalling){
				isFalling = false;
			}
			
		}else if(! "Structures".Equals(collisionObj.gameObject.tag) ){
			//Debug.Log ("OnCollisionEnter2D, collisionObj tag: " + collisionObj.gameObject.tag);
			//TODO: do additional thing if necessary
		}
	}
	void OnTriggerEnter2D(Collider2D collidedObj){
		string collidedObjTagName = collidedObj.gameObject.tag;
		Debug.Log("Monster onTriggerEnter(), collidedObj tag: " + collidedObjTagName);

		if( "Structures".Equals(collidedObjTagName) && "Ground".Equals(collidedObj.gameObject.name) ){
			isGrounded = true;
			if(isFalling){
				isFalling = false;
			}
			
		}else if(! "Structures".Equals(collidedObjTagName) ){
			//Debug.Log ("OnCollisionEnter2D, collisionObj tag: " + collisionObj.gameObject.tag);
			//TODO: do additional thing if necessary
		}
	}




	//Will be called by MagicBall.cs
	public void startDyingAnimation(){
		if(!isDying){
			isDying = true;
			dyingAnimationCountDown = 2f;
			//so that attack will not be "blocked"
			GetComponent<Collider2D>().enabled = false;
			//prevent gravity from pulling it down
			GetComponent<Rigidbody2D>().gravityScale = 0f;
		}
	}

	void removeMonsterFromScreen(){
		GetComponent<SpriteRenderer>().enabled = false;
		GetComponent<Animator>().enabled = false;
		//GetComponent<Rigidbody>().
	}

	int mobJump(){
		int curAction = 0;

		if(!isJump && isGrounded){
			
			//if jump jumpDelay ends
			if(moveDuration <= 0f){
				curAction = 2;
				isJump = true;
				isGrounded = false;
				countDown = jumpDuration;
				//jumpDelay = buttonjumpDelay_upArrow;
			}else{
				//if jump jumpDelay haven't end
				if( Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow) ){
					curAction = 1;
				}else{
					curAction = 0;
				}
			}
		}else{
			curAction = 2;
		}
		return curAction;
	}

	void animateIdle(){
		animator.SetInteger("animationType", 0);

	}
}//class
